package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.util.ValidateCodeUtils;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import org.apache.zookeeper.data.Id;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @PACKAGE_NAME: com.atguigu.controller
 * @CLASSNAME: UserInfoController
 * @AUTHOR: zhangsan
 * @DATE: 2024/4/27 11:02
 * @SINCE 17.0.7
 * @DESCRIPTION: UserInfoController
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController {

    @Reference
    private UserInfoService userInfoService;


    @RequestMapping("/sendCode/{phone}")
    public Result sendCode(@PathVariable("phone") String phone,
                           HttpServletRequest request) {
        //正则表达式校验手机号码
        String regex = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phone);
        if (!matcher.find()) {
            System.out.println("没找到手机号码");
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        } else {
            System.out.println(matcher.group(0));
        }
        String code = ValidateCodeUtils.generateValidateCode4String(4);
        //String code = "1111";
        //将code放入到session域中
        String regexCode = "\\w{4}";
        Pattern patternCode = Pattern.compile(regexCode);
        Matcher matcherCode = patternCode.matcher(code);
        if (matcherCode.find()) {
            HttpSession session = request.getSession();
            session.setAttribute("CODE", code);
            return Result.ok(code);
        } else {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
    }

    @RequestMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo, HttpServletRequest request) {
        //基本的参数校验,只校验类型,不校验格式
        String phone = registerVo.getPhone();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        String nickName = registerVo.getNickName();

        if (StringUtils.isEmpty(phone) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code) ||
                StringUtils.isEmpty(nickName)
        ) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //正则表达式校验手机号
        //不要加/ / 这是前端里面的写法.类似于后端的双引号,标注了正则表达式的作用范围,
        //因为不是字符串,所以里面的字符不用转义,写起来方便一些
        //String regex = "/^1[3456789]\\d{9}$/";
        String regex = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phone);
        if (!matcher.find()) {
            System.out.println("没找到手机号码");
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        } else {
            System.out.println(matcher.group(0));
        }

        //校验验证码
        String regexCode = "\\w{4}";
        Pattern patternCode = Pattern.compile(regexCode);
        Matcher matcherCode = patternCode.matcher(code);
        if (!matcherCode.find()) {
            System.out.println("没找到验证码");
            System.out.println(code);
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        } else {
            System.out.println("找到验证码了...");
            System.out.println(matcherCode.group(0));
        }

        HttpSession session = request.getSession();
        String sessionCODE = (String) session.getAttribute("CODE");
        if (sessionCODE == null || !code.equals(sessionCODE)) {
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }

        //查询该账号是否注册过
        //注册过,且没有注销
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if (null != userInfo && userInfo.getIsDeleted() == 0) {
            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        //查询该账户是否注销过
        if (null != userInfo && userInfo.getIsDeleted() != 0) {
            userInfo.setPhone(phone);
            userInfo.setPassword(MD5.encrypt(password));
            userInfo.setNickName(nickName);
            userInfo.setIsDeleted(0);
            userInfoService.update(userInfo);
        }

        //进行注册
        UserInfo newUserInfo = new UserInfo();
        newUserInfo.setId(null);
        newUserInfo.setPhone(phone);
        newUserInfo.setNickName(nickName);
        newUserInfo.setPassword(MD5.encrypt(password));
        newUserInfo.setStatus(1);
        Integer i = userInfoService.insert(newUserInfo);
        return Result.ok();
    }

    @RequestMapping("/login")
    public Result login(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();
        //参数校验
        if (StringUtils.isEmpty(phone) ||
            StringUtils.isEmpty(password)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        String regex = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phone);
        if (!matcher.find()) {
            System.out.println("没找到手机号码");
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        } else {
            System.out.println(matcher.group(0));
        }

        //查看该用户有没注册
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if (null == userInfo) {
            //该账号,没注册,或者不存在,请注册
            return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        }
        //校验密码
        String MD5Password = MD5.encrypt(password.trim());
        if (!MD5Password.equals(userInfo.getPassword().trim())) {
            //说明密码错误,
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
        //说明密码正确,接着
        //校验是否被禁用
        if (userInfo.getStatus() == 0) {
            //说明账户被禁用
            return Result.build(null, ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        //说明登录成功,且账户未被禁用,账户信息存入到session中
        HttpSession session = request.getSession();
        session.setAttribute("USER", userInfo);

        //返回参数
        return Result.ok(userInfo);
    }

    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("USER");
        return Result.ok();
    }
}
